package dao;
/**
 * 数据库连接,关闭的基础类
 * 
 *   打开连接的方法
 *   关闭连接的方法
 *   增删改公共的方法
 * 
 * @author Jane
 *
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class BaseDao {
	private final String dirver="com.mysql.jdbc.Driver";
	private final String url="jdbc:mysql://localhost:3306/financing"
			+ "?serverTimezone=GMT%2B8"
			+ "&useUnicode=true"
			+ "&characterEncoding=utf-8";

	private final String user ="root";
	private final String pwd ="123456";
	
	public Connection conn=null;  //数据库连接对象
	public PreparedStatement pstmt =null;
	public ResultSet rs =null;
	
	/**
	 * 获取数据库连接
	 * @return
	 */
	public Connection getConnection() {
		try {
			if(null == conn || conn.isClosed()) {
				Class.forName(dirver);
				conn = DriverManager.getConnection(url, user, pwd);
			}
			return conn;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 关闭数据连接
	 * @param connection
	 */
	public void closeConn(Connection connection,Statement stmt ,ResultSet rs) {
	
		if(null!=rs) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null!=stmt) {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null!=conn) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	} 
}
